package com.zxg.zbc.biz.manager.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alipay.domain.AlipayRequest;
import com.alipay.util.AlipayUtil;
import com.zxg.weixin.api.service.domain.WeixinUnifiedOrderRequest;
import com.zxg.weixin.api.service.domain.WeixinUnifiedOrderResponse;
import com.zxg.zbc.biz.exception.ManagerException;
import com.zxg.zbc.biz.manager.PayServiceManager;
import com.zxg.zbc.dal.dao.UserAliPayTradeDao;
import com.zxg.zbc.dal.dao.exception.DAOException;
import com.zxg.zbc.dal.dataobject.PayServiceItem;
import com.zxg.zbc.dal.dataobject.User;
import com.zxg.zbc.dal.dataobject.UserAlipayTrade;

public class PayServiceManagerImpl implements PayServiceManager {

  @Autowired
  UserAliPayTradeDao      userAliPayTradeDao;
  
 
  @Override
  public UserAlipayTrade generateAlipayFeeUrl(PayServiceItem item, User user) throws ManagerException {
    // 先生成本地订单
    UserAlipayTrade trade = new UserAlipayTrade();
    trade.setUserId(user.getId());
    trade.setPayServiceId(item.getId().longValue());
    trade.setId(UserAlipayTrade.generateId(user.getId()));
    // trade.setType(item.getType());
    try {
      long tradeId = userAliPayTradeDao.insert(trade);
      if (item.getAlipayBody() == null)
        item.setAlipayBody("换包裹费用代付");
      if (item.getAlipaySubject() == null)
        item.setAlipaySubject("换包裹费用代付！");
      else
        item.setAlipaySubject(item.getAlipaySubject() + "_" + user.getUserNick());

      AlipayRequest request = new AlipayRequest("" + tradeId, item.getAlipaySubject(), item.getFee() + "", item.getAlipayBody());
      trade.setAlipayRequestUrl(AlipayUtil.generatePayUrl(request));
      return trade;
    } catch (Exception e) {
      log.error("生成本地订单错误", e);
      throw new ManagerException("生成本地订单错误", e);
    }
  }

  /**
   * 处理支付宝返回的支付信息（可能有Bug同步问题 暂时忽略） 1 根据out_trade_no 找到本地 UserAlipayTrade 2 如果
   * UserAlipayTrade的processed为false 则2.1 否则 2,2 2.1 立即更新processed 状态为true
   * 处理业务逻辑 2.2 直接忽略 不处理
   * 
   * @param success
   * @param out_trade_no
   * @param trade_no
   * @param trade_status
   * @param buyer_email
   * @return
   */
  @Override
  public UserAlipayTrade generatePayResult(Boolean success, String out_trade_no, String trade_no, String trade_status, String buyer_email, String totalFee)
      throws ManagerException {
   return null;
  }

 

  public static void main(String[] args) {
    UserAlipayTrade trade = new UserAlipayTrade();
    trade.setUserId(1l);
    trade.setPayServiceId(1l);
    trade.setId(UserAlipayTrade.generateId(1l));

    AlipayRequest request = new AlipayRequest("111222", "dsfdffd", 1 + "", "dsdfd");
    System.out.println(AlipayUtil.generatePayUrl(request));
  }

  @Override
  public List<UserAlipayTrade> getTradesByUserId(User user) throws ManagerException {

    try {
      return this.userAliPayTradeDao.getTradesByUserId(user.getId());
    } catch (DAOException e) {
      log.error("getTradesByUserId错误", e);
      throw new ManagerException("getTradesByUserId错误", e);
    }
  }

  @Override
  public String generateYoufeiAlipayUrl(Long billId, String fee, User user) throws ManagerException {
    return null;
  }

  @Override
  public Map<String, String> generateYoufeiAlipayUrlWeixin(Long billId, String fee, User user, String openId, String ip) throws ManagerException {

    return null;
  }

  @Override
  public WeixinUnifiedOrderResponse generateWeixinUnifiedOrder(PayServiceItem item, User user, String openId, String ip) throws ManagerException {

    UserAlipayTrade trade = new UserAlipayTrade();
    trade.setUserId(user.getId());
    trade.setPayServiceId(item.getId().longValue());
    trade.setId(UserAlipayTrade.generateId(user.getId()));
    // trade.setType(item.getType());
    try {
      long tradeId = userAliPayTradeDao.insert(trade);
      if (item.getAlipayBody() == null)
        item.setAlipayBody("纸箱哥广告投放费用！");
      if (item.getAlipaySubject() == null)
        item.setAlipaySubject("纸箱哥广告投放费用！");
      else
        item.setAlipaySubject(item.getAlipaySubject() + "_" + user.getMobile());

      WeixinUnifiedOrderRequest request = new WeixinUnifiedOrderRequest(openId, Integer.parseInt(item.getFee()), tradeId + "", item.getAlipaySubject(), ip,
          "http://media.zhixiangge.com/weixinNotify.htm");
      //WeixinUnifiedOrderResponse response = this.weixinPayService.doWeixinPayUnifiedOrder(request);
      return null;
    } catch (Exception e) {
      log.error("生成本地订单错误", e);
      throw new ManagerException("生成本地订单错误", e);
    }

  }

  

  @Override
  public boolean handleAlipayResult(boolean succeed, String alipayId, String alipayTradeNo, String tradeStatus, String buyeAlipayEmail, String totalFee){
     return false;
  }
 
}
